<?php
if( (!isset($_SESSION['next'])) || ($_SESSION['next'] < 3) )
{
	echo '<script laguage="javascript"> 
              window.location.href = "../installation/";
          </script>';
}
//status = 0: khong co loi, 1: co loi
$status = 0;
$message = '';
$username = '';
$password = '';
$repassword = '';
$fullname   = '';
if(isset($_POST['username']))
{
	$repassword     = trim($_POST['repassword']);
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$fullname   = trim($_POST['fullname']);
	if(($fullname == '') || ($username == '') || ($password == '') || ($repassword == ''))
	{
		$status = 1;
		$message = "Nhập thiếu thông tin";
	}
	
	if(($status == 0) && ($password != $repassword))
	{
		$status = 1;
		$message = "Mật khẩu không khớp.";
	}
	
	if($status == 0)
	{
		include_once 'functions/Database.php';
		$fileConfig   = SITE_PATH.'/application/configs/config.php';
		include_once($fileConfig);
		$db_host = $config['database']['host'];
		$db_username = $config['database']['username'];
		$db_password = $config['database']['password'];
	    $db_dbname   = $config['database']['dbname'];	
		$db = new Database($db_host, $db_dbname, $db_username, $db_password);
		if($db->getPDO() == null)
		{		
			$status = 1;
			$message = "Không kết nối được tới CSDL.";
		}
		else
		{
			$db->execSQL("Delete * from admin");
			$sql = 'insert into admin(username,password,fullname) values (:username,PASSWORD(:password),:fullname)';
    		$sth = $db->getPDO()->prepare($sql);
    		$sth->bindParam(':username', $username, PDO::PARAM_STR);
    		$sth->bindParam(':password', $password, PDO::PARAM_STR);
    		$sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    	    if($sth->execute())
    	    {
    	    	$_SESSION['next'] = 4;
				echo '<script laguage="javascript"> 
             		 window.location.href = "../installation/?frame=configuration";
          		  	</script>';
    	    }
    	    else
    	    {
    	    	$status = 1;
				$message = "Không thêm được tài khoản admin vào CSDL.";
    	    }
		}
	}
	
	
	if($status == 0)
	{
		$db = new Database($host, "", $username, $password);
		if($db->getPDO() != null)
		{
			$db->createDatabase($dbname);
			$db1 = new Database($host, $dbname, $username, $password);
			$fileSql = INSTALL_PATH.'/sql/bkloud.sql';
			$content = file_get_contents($fileSql);
			$db1->importSql($content);
			$_SESSION['next'] = 3;
			echo '<script laguage="javascript"> 
             		 window.location.href = "../installation/?frame=account";
          		  </script>';
		}
		else 
			{
				$status = 1;
				$message = "Kết nối cơ sở dữ liệu thất bại.";
			}
	}
}
?>

       <script>
        	addActive('account');
        	
        </script>
    	<div id="topright">
        	<div id="topright1">
        		<span> Cấu hình website </span> 
            </div>
            <div id="topright2">
            	<button id="next" onclick="document.forms['account'].submit()">
                </button>
            </div> 
    	</div> 
        
    	<div id="botright">
			<div id="botright1">
				  <form id="account" action="../installation/?frame=account" method="post">
     				 <?php
        				if($status == 1)
        				{
      				?>      
      				<div style="padding-bottom:15px; color:#FF0000;width:400px;font-weight:bold;">
      				<img src="images/error.png" width="32" height="32" style="float:left; margin-right:10px;"/>
      				Lỗi : <?php echo $message;?>
      				</div>
      				<?php
       				}
      				?>
			      	<p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Tài khoản admin</label>
			        	<input type="text" name="username" style="padding: 5px;width: 300px;" value="<?php echo $username;?>"/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Mật khẩu</label>
			        	<input type="password" name="password" style="padding: 5px;width: 300px;" value=""/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Nhập lại mật khẩu</label>
			        	<input type="password" name="repassword" style="padding: 5px;width: 300px;" value=""/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Họ tên</label>
			        	<input type="text" name="fullname" style="padding: 5px;width: 300px;" value="<?php echo $fullname;?>"/>
			       </p>
			     </form> 
			</div>       
			<div id="botright2">
			  <p>Tạo tài khoản admin</p>
			  <p>Các trường thông tin đều là bắt buộc</p>
			</div>
    	</div>